﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class adminsys : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    string User = "";
                    if (Request.QueryString["beginDate"]!=null && Request.QueryString["endDate"] != null)
                    {
                        txtBeginDate.Text = Request.QueryString["beginDate"];
                        txtEndDate.Text = Request.QueryString["endDate"];
                    }
                    BindListDelivery();
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }

      
        private void BindListDelivery()
        {
            int pageSize = 10; // so san pham tren 1 trang
            int total =0;
            try
            {
                //input begin date and end date
                if(!txtBeginDate.Text.Equals("") && !txtEndDate.Text.Equals(""))
                {
                    total = Business.DeliveryService.Delivery_GetCountByWhere("DateDiff(day,'"+txtBeginDate.Text+"',DeliveryDate)>=0 and DateDiff(day,DeliveryDate,'"+txtEndDate.Text+"')>=0");
                    
                }
                //input only begin date
                else if (!txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
                {
                    total = Business.DeliveryService.Delivery_GetCountByWhere("DateDiff(day,'" + txtBeginDate.Text + "',DeliveryDate)>=0");
                   
                }
                //input only end date
                else if (txtBeginDate.Text.Equals("") && !txtEndDate.Text.Equals(""))
                {
                    total = Business.DeliveryService.Delivery_GetCountByWhere("DateDiff(day,DeliveryDate,'" + txtEndDate.Text + "')>=0");
                   
                }
                //don't input begin date and end date
                else
                {
                    total = Business.DeliveryService.Delivery_GetCountByWhere("");
                }
            }
            catch { }

            int pages = total / pageSize; // so trang

            if (total % pageSize > 0)
                pages++;

            int currentPage = 1; // trang hien tai

            if (Request.QueryString["page"] != null)
            //if(RouteData.Values["page"]!=null)
            {
                try
                {
                    currentPage = Convert.ToInt32(Request.QueryString["page"]);
                   //currentPage = Convert.ToInt32(RouteData.Values["page"]);
                }
                catch
                {
                    Response.Redirect("/AdminIstrator/ListDelivery.aspx");
                    currentPage = 1;
                }
            }

            if (currentPage <= 1)
            {
                ltrPaging.Text = "<span class=\"disabled\"><< prev</span>";
                //Response.Redirect("URLRouting.aspx");
            }
            else
            {
                if (txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
                {
                    ltrPaging.Text = "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + (currentPage - 1) + "\"><< prev</a>";
                    //ltrPaging.Text = "<a href=\"/AdminIstrator/ListDelivery/p" + (currentPage - 1) + ".admin\"><< prev</a>";
                }
                else
                {
                    ltrPaging.Text = "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + (currentPage - 1) + "&beginDate="+txtBeginDate.Text+"&endDate="+txtEndDate.Text+"\"><< prev</a>";
                    //ltrPaging.Text = "<a href=\"/AdminIstrator/ListDelivery/p" + (currentPage - 1) + "/" + txtBeginDate.Text + "/" + txtEndDate.Text + ".admin\"><< prev</a>";
                }
            }

            for (int i = 1; i <= pages; i++)
            {
              
                if ((i > 3) && (currentPage - i > 3 && currentPage-i<10 || (currentPage-i>12)) || (i - currentPage > 3 && i-currentPage<10 || (i - currentPage>12))  && i < pages-2)
                {
                    continue;
                }
                if (currentPage == i)
                    ltrPaging.Text += "<span class=\"current\">" + i + "</span>";
                else
                {
                    if (txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
                        ltrPaging.Text += "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + i + "\">" + i + "</a>";
                        //ltrPaging.Text += "<a href=\"/AdminIstrator/ListDelivery/p" + i + ".admin\">" + i + "</a>";

                    else
                        ltrPaging.Text += "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + i + "&beginDate="+txtBeginDate.Text+"&endDate="+txtEndDate.Text+"\">" + i + "</a>";
                    
                }

               
            }

            if (currentPage >= pages)
            {
                ltrPaging.Text += "<span class=\"disabled\">next >></span>";
                //Response.Redirect("/AdminIstrator/ListDelivery.aspx?page=4");
            }
            else
            {
                if (txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
                    ltrPaging.Text += "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + (currentPage + 1) + "\">next >></a>";
                else 
                    ltrPaging.Text += "<a href=\"/AdminIstrator/ListDelivery.aspx?page=" + (currentPage + 1) + "&beginDate=" + txtBeginDate.Text + "&endDate=" + txtEndDate.Text + "\">next >></a>";
            }

            if (currentPage <= 0)
            {
                currentPage = 1;
                Response.Redirect("/AdminIstrator/ListDelivery.aspx");
            }
            //try
            //{
                if (!txtBeginDate.Text.Equals("") && !txtEndDate.Text.Equals(""))
                {
                    
                    rpDelivery.DataSource = Business.DeliveryService.Delivery_GetByWherePaging(currentPage.ToString(), pageSize.ToString(), "DateDiff(day,'" + txtBeginDate.Text + "',DeliveryDate)>=0 and DateDiff(day,DeliveryDate,'" + txtEndDate.Text + "')>=0");
                }
                else if (!txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
                {
                    rpDelivery.DataSource = Business.DeliveryService.Delivery_GetByWherePaging(currentPage.ToString(), pageSize.ToString(), "DateDiff(day,'" + txtBeginDate.Text + "',DeliveryDate)>=0");
                }
                else if (txtBeginDate.Text.Equals("") && !txtEndDate.Text.Equals(""))
                {
                    rpDelivery.DataSource = Business.DeliveryService.Delivery_GetByWherePaging(currentPage.ToString(), pageSize.ToString(), "DateDiff(day,DeliveryDate,'" + txtEndDate.Text + "')>=0");
                }
                else
                {
                    rpDelivery.DataSource = Business.DeliveryService.Delivery_GetByWherePaging(currentPage.ToString(), pageSize.ToString(), "");
                }
            //}
            //catch { }
            rpDelivery.DataBind();
            for (int i = 0; i < rpDelivery.Items.Count; i++)
			{
                DropDownList ddlStatus = (DropDownList)rpDelivery.Items[i].FindControl("ddlStatus");
                Label lblStatus = (Label)rpDelivery.Items[i].FindControl("lblStatus");
                if (lblStatus.Text.Equals("0"))
                {
                    ddlStatus.SelectedIndex = 0;
                }
                else if (lblStatus.Text.Equals("1"))
                {
                    ddlStatus.SelectedIndex = 1;
                }
                else if (lblStatus.Text.Equals("2"))
                {
                    ddlStatus.SelectedIndex = 2;
                }
                else if(lblStatus.Text.Equals("3")||lblStatus.Text.Equals("4"))
                {
                    ddlStatus.SelectedIndex=Convert.ToInt32(lblStatus.Text);
                    ((Button)rpDelivery.Items[i].FindControl("btnStatus")).Visible = false;
                }

                Label lblPrice = (Label)rpDelivery.Items[i].FindControl("lblPrice");
                
                lblPrice.Text = Format_Price(lblPrice.Text.Replace(".0000",""));
			}
            
            //rpDelivery.DataSource = Business.DeliveryService.Delivery_GetByAll();
            //rpDelivery.DataBind();
        }

       

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            
            BindListDelivery();
            Response.Redirect("/AdminIstrator/ListDelivery.aspx?beginDate="+txtBeginDate.Text+"&endDate="+txtEndDate.Text);
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            if (rpDelivery.Items.Count > 0)
            {
                for (int i = 0; i < rpDelivery.Items.Count; i++)
                {
                    CheckBox chk = (CheckBox)rpDelivery.Items[i].FindControl("chkSelect");
                    Label lblId = (Label)rpDelivery.Items[i].FindControl("lblDeliveryId");
                    if (chk.Checked)
                    {
                        Business.DeliveryService.Delivery_DeleteById(lblId.Text);
                    }
                }
                BindListDelivery();
            }
        }

        protected void lbtViewAll_Click(object sender, EventArgs e)
        {
            
            Response.Redirect("/AdminIstrator/ListDelivery.aspx");
            //BindListDelivery();
        }

        protected void rpDelivery_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string Id = e.CommandArgument.ToString();
            if (e.CommandName.Equals("Approve"))
            {
                for (int i = 0; i < rpDelivery.Items.Count; i++)
                {
                    
                    Label lblID = (Label)rpDelivery.Items[i].FindControl("lblDeliveryId");
                    if (lblID.Text.Equals(Id))
                    {
                        DropDownList ddl = (DropDownList)rpDelivery.Items[i].FindControl("ddlStatus");
                        if (ddl.SelectedIndex == 3||ddl.SelectedIndex==4)
                        {
                            if (ddl.SelectedIndex == 4)
                            {
                                List<Data.DeliveryDetailInfo> listD = Business.DeliveryDetailService.DeliveryDetail_GetByAll().FindAll(dd=>dd.DeliveryId==Id);
                                for (int j = 0; j < listD.Count; j++)
                                {
                                    Data.SqlDataProvider Conn = new Data.SqlDataProvider();
                                    Conn.ExecuteNonQuery("update Products set PNumber=" + listD[i].Qty + " where PId=" + listD[i].ProductId);
                                }

                            }
                            Business.DeliveryService.Delivery_UpdateById("DeliveryStatus = " + ddl.SelectedIndex + ", DeliveryApprovedDate= null", Id);
                            
                        }
                        else if (Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryId == lblID.Text).DeliveryStatus != "0")
                        {
                            if (ddl.SelectedIndex == 0)
                            {
                                Response.Write("<script>alert('Đờn hàng đã được xử lý, không thể cập nhật'); window.location.href='/AdminIstrator/ListDelivery.aspx';</script>");
                            }
                            else
                            {


                                Business.DeliveryService.Delivery_UpdateById("DeliveryStatus = " + ddl.SelectedIndex + ", DeliveryApprovedDate= null", Id);

                            }
                        }
                        else
                        {
                            Business.DeliveryService.Delivery_UpdateById("DeliveryStatus = " + ddl.SelectedIndex + ", DeliveryApprovedDate= null", Id);
                        }
                        break;
                    }
                }
               
                BindListDelivery();
            }
        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
    }
}